package org.jackysoft.struts2.fileserver;
import java.io.ByteArrayInputStream;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.HistoryFileItem;
import org.jackysoft.service.HistoryFileItemService;
import org.jackysoft.struts2.UtilsCRUDAction;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;



@Namespace("/historyfileitem")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class HistoryFileItemAction extends UtilsCRUDAction<HistoryFileItem> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2348718722413418419L;
	private HistoryFileItemService service;
	private HistoryFileItem bean;
	private Collection<HistoryFileItem> beans;	
 
	
	public String save() throws Exception{
		
		service.save(search);
	
		return "save";
	}
	
	
	public String openhistory() throws Exception{
		bean = service.openHistoryDoc(bean.getId());
		upLoadFileName = StringUtils.toDownloadFileName(bean.getTitle()+"."+bean.getFileType());
		contentLength = bean.getData().length;
		contentType = HttpUtil.extToMimeType(bean.getFileType());
		inputStream = new ByteArrayInputStream(bean.getData());
	    return STREAM;
	}
	
	public String get() throws Exception{
		bean = service.get(bean.getId());
		bean.setOpenUrl("historyfileitem/history-file-item!openhistory?bean.id="+bean.getId());
		bean.setSaveUrl("historyfileitem/history-file-item!save");
		return "get";
	}	
		
	public void setBean(HistoryFileItem bean){
	     this.bean = bean;
	}
	public HistoryFileItem getBean(){
	     return this.bean;
	}
	public void setBeans(Collection<HistoryFileItem> beans){
	
	     this.beans = beans;
	}
	public Collection<HistoryFileItem> getBeans(){
	     return this.beans;
	}
	
	@Resource
	public void setService(HistoryFileItemService service) {
		this.service = service;
		this.setDataProvider(service);
	}	
	
	public HistoryFileItemService getService(){
	   return this.service;
	}
		
}